package leetcode.sort;

/**
 * @author 榆关
 **/
public class findLongestString {

    public static void main(String[] args) {
        String input = "abpcplea";
        String[] d = {"ale","apple","monkey","plea"};
        System.out.println(solution(input,d));
    }
    public static String solution(String input,String[] dict){
        String longest = "";
        for(String dic: dict){
            int i = 0;
            for(char c: input.toCharArray()){
                if(i<dic.length() && c == dic.charAt(i)){
                    i++;
                }
                if(i == dic.length() && dic.length() >= longest.length()){
                    if(dic.length() > longest.length() || dic.compareTo(longest) < 0){
                        longest = dic;
                    }
                }
            }
        }
        return longest;
    }
}
